#!/bin/bash
source ./constants
set -ex

# 配置git，防止下载代码失败
sudo apt-get install gnutls-bin
git config --global user.name "xwx1135370"
git config --global user.email "xuyao44@huawei.com"
git config --global http.sslVerify false
git config --global http.postBuffer 9048576000


# 测试套路径/home/xwx1135370/workspace/Testing
WORK_DIR=${PWD}
# 清空历史记录
if [ -d ${WORK_DIR}/llvm ] ;then
    rm -rf ${WORK_DIR}/llvm
fi

mkdir -p ${WORK_DIR}/llvm

# 下载llvm代码
cd ${WORK_DIR}/llvm
repo init -u https://gitee.com/OpenHarmony/manifest.git -b master -m llvm-toolchain.xml
repo sync -c 
repo forall -c 'git lfs pull'

# 准备工具
./toolchain/llvm-project/llvm-build/env_prepare.sh

# 编译llvm工具链
python3 ./toolchain/llvm-project/llvm-build/build.py

# 编译出来的工具链所在位置
# /home/xwx1135370/workspace/Testing/llvm/out/

# llvm-ci仓下载
git clone https://gitee.com/xwx1135370/llvm-ci.git -b master

sudo chown -R $USER ${LLVM_PROJECT_DIR} *

# HOS平台
function kill-lldb-server() {
    adb shell killall -9 lldb-server || true
    adb shell ps -A | grep lldb_server && ( echo "lldb server wasn't killed";  exit 1 ) || true
}

# 半容器
#function kill-lldb-server() {
#    hdc shell ohsh killall -9 lldb-server || true
#    # 半容器selinux和去root之后,ps 是查不到lldb-server进程的
#    #hdc shell ohsh ps -A | grep lldb_server && ( echo "lldb server wasn't killed";  exit 1 ) || true
#}

# OHOS平台
#function kill-lldb-server() {
#    hdc shell killall -9 lldb-server || true
#    hdc shell ps -A | grep lldb_server && ( echo "lldb server wasn't killed";  exit 1 ) || true
#}

# TODO_FIX_HACK: need somehow modify "Makefile.rules" to not to include "Android.rules" file
sed -i 's/\(include $(THIS_FILE_DIR)\/Android.rules\)/#\1/g' \
    ${LLDB_PROJECT_DIR}/packages/Python/lldbsuite/test/make/Makefile.rules

export ANDROID_SERIAL=${DEVICE_SERIAL}
if [[ ! "${ANDROID_SERIAL}" ]]; then
    # lldb will fail if phone serial will be with colon character in platform_url parameter.
    # It will fail because it expect tcp port number of lldb-server(for example 1234) on remote side,
    # and url like "main-provider.devops-spb.rnd.huawei.com:15121:1234" does not parse correctly.
    # The only way to workaround this is to set serial as environment variable and set only lldb-server
    # port without address in platform_url argument
    false
fi

kill-lldb-server

# HOS平台
adb forward tcp:${LLDB_SERVER_PORT} tcp:${LLDB_SERVER_PORT}
adb shell "rm -rf ${REMOTE_PLATFORM_WORKDIR} && mkdir ${REMOTE_PLATFORM_WORKDIR}"
adb push ${TOOLCHAIN_BIN_PATH}/${LLDB_SERVER_BINARY_RELATIVE_TO_TOOLCHAIN_PATH} ${REMOTE_PLATFORM_WORKDIR}
SERVER_LOGFILE=lldb_server.log
adb shell \
    ${REMOTE_PLATFORM_WORKDIR}/lldb-server \
    platform \
    --listen "*:${LLDB_SERVER_PORT}" \
    --server  > ${SERVER_LOGFILE} 2>&1 &
adb_lldb_server_pid=$!
# to check adb lldb-server run job status (job shoud be in 'running' state)
jobs

# 半容器
#hdc fport tcp:${LLDB_SERVER_PORT} tcp:${LLDB_SERVER_PORT}
#hdc shell "rm -rf ${REMOTE_PLATFORM_WORKDIR} && mkdir ${REMOTE_PLATFORM_WORKDIR}"
#hdc pull ${TOOLCHAIN_BIN_PATH}/${LLDB_SERVER_BINARY_RELATIVE_TO_TOOLCHAIN_PATH} ${REMOTE_PLATFORM_WORKDIR}
#SERVER_LOGFILE=lldb_server.log
#hdc shell ohsh\
    ${CONTAINER_REMOTE_PLATFORM_WORKDIR}/lldb-server \
    platform \
    --listen "*:${LLDB_SERVER_PORT}" \
    --server  > ${SERVER_LOGFILE} 2>&1 &
#hdc_lldb_server_pid=$!

CONFIG_OUT="${LLDB_PROJECT_DIR}"
python3 "${LLVM_CI_SCRIPTS}"/generate_config.py \
        --target "${TARGET}" \
        --default "${LLVM_CI_CONFIGS}/${CONFIG}" \
        --clang "${TOOLCHAIN_PATH}" \
        --sysroot "${SYSROOT}" \
        --out "${LLDB_PROJECT_DIR}"

CONFIG_FILE="${CONFIG_OUT}/${TARGET}.cfg"
CONFIG_OPT="--config ${CONFIG_FILE}"

echo ${CONFIG_FILE}
cat ${CONFIG_FILE}
LOGFILE="${LLDB_PROJECT_DIR}/test.log"

pushd "${LLVM_PROJECT_DIR}"

# clear previous cmake files
rm -rf CMake* *.cmake

cmake -G Ninja \
    -DLLVM_ENABLE_PROJECTS='clang;clang-tools-extra;libcxx;libcxxabi;libunwind;lldb;compiler-rt;lld;polly' \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="${PWD}/install" \
    "${LLVM_PROJECT_DIR}/llvm"

# just generated
ninja llvm-tblgen
LLVM_DIR="${PWD}/lib/cmake/llvm"

# clear previous cmake files
rm -rf CMake* *.cmake

popd

# clear previous cmake files
rm -rf CMake* *.cmake

LLDB_XML_TEST_OUTPUT="test-results.xml"
disabled_tests_dir="${LLVM_CI_RESOURCES}/disabled_tests"

    LLDB_TEST_USER_ARGS="-v;"
    LLDB_TEST_USER_ARGS+="--arch;${LLDB_API_DOTEST_ARCH};"
    LLDB_TEST_USER_ARGS+="-E;${CONFIG_OPT};"
    LLDB_TEST_USER_ARGS+="--env;ARFLAGS=rvU;"
    #LLDB_TEST_USER_ARGS+="--excluded;${disabled_tests_dir}/${DISABLED_TESTS};"
    LLDB_TEST_USER_ARGS+="--platform-name;${LLDB_API_REMOTE_PLATFORM_NAME};"
    LLDB_TEST_USER_ARGS+="--platform-url;${LLDB_API_REMOTE_PLATFORM_URL_STRING};"
    LLDB_TEST_USER_ARGS+="--platform-working-dir;${REMOTE_PLATFORM_WORKDIR};"

    cmake \
    -DCMAKE_C_COMPILER="${TOOLCHAIN_BIN_PATH}/clang" \
    -DCMAKE_CXX_COMPILER="${TOOLCHAIN_BIN_PATH}/clang++" \
    -DLLVM_DIR="${LLVM_DIR}" \
    -DLLDB_DEFAULT_TEST_ARCH="${LLDB_API_DOTEST_ARCH}" \
    -DCMAKE_AR="${TOOLCHAIN_BIN_PATH}/llvm-ar" \
    -DCMAKE_OBJCOPY="${TOOLCHAIN_BIN_PATH}/llvm-objcopy" \
    -DLLVM_TABLEGEN_EXE="${LLVM_PROJECT_DIR}/bin/llvm-tblgen" \
    -DLLDB_TEST_EXECUTABLE="${TOOLCHAIN_BIN_PATH}/lldb" \
    -DLLDB_TEST_COMPILER="${TOOLCHAIN_BIN_PATH}/clang" \
    -DLLDB_TEST_DSYMUTIL="${TOOLCHAIN_BIN_PATH}/dsymutil" \
    -DLLDB_TEST_FILECHECK="${TOOLCHAIN_BIN_PATH}/FileCheck" \
    -DLLDB_TEST_YAML2OBJ="${TOOLCHAIN_BIN_PATH}/yaml2obj" \
    -DLLDB_TEST_SERVER="${REMOTE_PLATFORM_WORKDIR}/lldb-server" \
    -DLLDB_TEST_USER_ARGS="${LLDB_TEST_USER_ARGS}" \
    "${LLDB_PROJECT_DIR}"

    
    rm -rf lldb-test-build.noindex

    lit --xunit-xml-output "${LLDB_XML_TEST_OUTPUT}" --show-all -v "${LLDB_TEST_PATH}" --timeout=300

    # hos平台
    kill ${adb_lldb_server_pid} || true
    kill-lldb-server

    #clean-up
    #adb shell "rm -rf ${REMOTE_PLATFORM_WORKDIR}" || true
